﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wicresoft.UnifyShow.Business;
using Oracle.DataAccess.Client;
using System.Data;

namespace Wicresoft.UnifyShow.BusinessImpl
{
    public class BusinessAvailable : Basic, IBusinessAvailable
    {
        public const string sqlGetSystemAvailableByBusinessSystemId = "SELECT b.timestamp,avg(b.availability) availability FROM  businessavailable b WHERE b.businesssystemid=:businesssystemid "
           + " AND b.TIMESTAMP>=:start1 AND b.TIMESTAMP<=:end1 group by b.timestamp ";
        //public List<SiteBranchAndSystemAvailableModel> getSiteBranchAndSystemAvailableByBusinessSystemId(int businessSystemId, int userID)
        //{
        //    List<SiteBranchAndSystemAvailableModel> list = new List<SiteBranchAndSystemAvailableModel>();
        //    string sqlStr = string.Concat(" select t3.Name,t3.Alias,t2.ID,t1.Availability,t1.Timestamp,t1.CreateTime from  BusinessSystem t2 ",
        //        " inner join SiteBranch t3 on t2.SiteID=t3.ID and t2.SiteID in ( select siteid from usersite where userid = :userID ) left join BusinessAvailable t1 on t1.BusinessSystemId=t2.ID where t2.ID = :businessSystemId ");
        //    List<OracleParameter> cmdParms = new List<OracleParameter>();
        //    cmdParms.Add(new OracleParameter("userID", OracleDbType.Int32, userID, ParameterDirection.Input));
        //    cmdParms.Add(new OracleParameter("businessSystemId", OracleDbType.Int32, businessSystemId, ParameterDirection.Input));
        //    OracleDataReader odr = oda.ExecuteReader(sqlStr, cmdParms);
        //    list = ModelHelper<SiteBranchAndSystemAvailableModel>.GetModel_By_OracleDataReader(odr);
        //    return list;
        //}

        public List<BusinessAvailableModel> GetSystemAvailableByBusinessSystemId(int businessSystemId, DateTime start, DateTime end)
        {
            return ModelHelper<BusinessAvailableModel>.GetModel_By_DirectSql(sqlGetSystemAvailableByBusinessSystemId, new List<OracleParameter>()
               {
               new OracleParameter()
                {
                    ParameterName = "businesssystemid",
                    OracleDbType = OracleDbType.Decimal,
                    Value = businessSystemId
                },
                new OracleParameter()
                {
                    ParameterName = "start1",
                    OracleDbType = OracleDbType.Date,
                    Value = start
                },
                  new OracleParameter()
                {
                    ParameterName = "end1",
                    OracleDbType = OracleDbType.Date,
                    Value = end
                }
               }, oda, new List<string>() { "availability", "timestamp" });
        }


        //public List<SiteBranchAndSystemAvailableModel> getSiteAndSystemBySystemName(int businessSystemId, int userID, DateTime? timeStart, DateTime? timeEnd)
        //{
        //    List<SiteBranchAndSystemAvailableModel> list = new List<SiteBranchAndSystemAvailableModel>();
        //    string whereStr = string.Empty;
        //    List<OracleParameter> cmdParms = new List<OracleParameter>();
        //    cmdParms.Add(new OracleParameter("userID", OracleDbType.Int32, userID, ParameterDirection.Input));
        //    cmdParms.Add(new OracleParameter("businessSystemId", OracleDbType.Int32, businessSystemId, ParameterDirection.Input));
        //    if (timeStart != null)
        //    {
        //        whereStr += " and timestamp >= :timeStart ";
        //        cmdParms.Add(new OracleParameter("timeStart", OracleDbType.Date, timeStart, ParameterDirection.Input));
        //    }
        //    if (timeEnd != null)
        //    {
        //        whereStr += "and timestamp <= :timeEnd  ";
        //        cmdParms.Add(new OracleParameter("timeEnd", OracleDbType.Date, timeEnd, ParameterDirection.Input));
        //    }
        //    string sqlStr = string.Concat("select tb.sitename,tb.sitealias,tb.sysid,tb.sysname, avg(tb.Availability) Availability from ( ",
        //        " select t3.Name sitename,t3.Alias sitealias,t2.ID sysid,t2.Name sysname,t1.Availability from  BusinessSystem t2 " ,
        //        " inner join SiteBranch t3 on t2.SiteID=t3.ID and t2.SiteID in (select siteid from usersite where userid = :userID) ",
        //        " inner join BusinessAvailable t1 on t1.BusinessSystemId=t2.ID ",
        //        " where t2.name in  (select name from BusinessSystem t4 where t4.ID = :businessSystemId) ", whereStr,
        //        " ) tb group by tb.sitename,tb.sitealias,tb.sysid,tb.sysname");
        //    OracleDataReader odr = oda.ExecuteReader(sqlStr, cmdParms);

        //    list = ModelHelper<SiteBranchAndSystemAvailableModel>.GetModel_By_OracleDataReader(odr);
        //    return list;
        //}
    }
}
